package org.pjlib.common;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;

import org.pjlib.R;


/**
 * Created by ws on 2019/05/24.
 */
public class UpdataDialog extends BaseDialog {
    public UpdataDialog(Context context) {
        super(context, R.style.alert_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static class Builder {
        private Context ctx;
        private UpdataDialog dialog;
        private BViewHolder holder;
        private int icon;
        private String title;
        private String message;
        private boolean cancelable = false;
        private boolean canceledOnTouchOutside = false;
        private ProgressBar progressBar;

        public void setProgress(int progress) {
            if (progressBar != null) {
                progressBar.setProgress(progress);
            }
        }

        public Builder(Context ctx) {
            this.ctx = ctx;
        }

        public Builder setIcon(int icon) {
            this.icon = icon;
            if (holder != null) {
                if (icon > 0) {
                    holder.image(R.id.iv_icon, icon)
                            .visible(R.id.iv_icon);
                }
            }
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            if (holder != null) {
                holder.text(R.id.tv_title, title);
            }
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            if (holder != null) {
                holder.text(R.id.tv_msg, message);
            }
            return this;
        }

        public Builder setQuitBottom(String str, final OnClickListener onClickListener) {
            if (holder != null) {
                holder.text(R.id.tv_single_quit, str)
                        .visible(R.id.tv_single_quit)
                        .clicked(R.id.tv_single_quit, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dismiss();
                                if(onClickListener!=null){
                                    onClickListener.onClick(dialog,0);
                                }
                            }
                        });
            }
            return this;
        }

        public void dismiss() {
            if (dialog != null) {
                dialog.dismiss();
            }
        }

        public UpdataDialog builder() {
            dialog = new UpdataDialog(ctx);
            View rootView = LayoutInflater.from(ctx).inflate(R.layout.dialog_updata, null);
            dialog.setContentView(rootView);
            holder = new BViewHolder(rootView);
            holder.text(R.id.tv_title, title)
                    .text(R.id.tv_msg, message);
            if (icon > 0) {
                holder.image(R.id.iv_icon, icon)
                        .visible(R.id.iv_icon);
            }
            dialog.setCancelable(cancelable);
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
            progressBar = holder.findViewById(R.id.progressBar);
            progressBar.setProgress(0);
            return dialog;
        }


        public void show() {
            builder().show();
        }
    }
}
